def register():
    '''
    注册
    :return:
    '''
    global usertable
    print("请输入您要注册的用户名:")
    useradd = input()
    print("请输入您要注册的密码:")
    passwdadd = input()
    usertable[useradd] = passwdadd
    print("恭喜你注册成功")


def login():
    '''
    登录
    :return:
    '''
    global usertable
    print("请输入您要登录的用户名:")
    user = input()
    print("请输入您要登录的密码:")
    passwd = input()
    if user in usertable:
        x = usertable.get(user)
        if x == passwd:
            print("登录成功！")
            global index
            global nowuser
            index = 1
            nowuser = user
            return
    print("登录失败！请检查用户名和密码或注册账户")



def commodity_add():
    print("请输入商品编号：")
    global num
    num = input()
    print("请输入商品名：")
    name = input()
    print("请输入商品价格：")
    price = input()
    print("请输入商品数量：")
    global count
    count = input()
    global commodityinf
    commodityinf[num] = {
        'name': name,
        'price': price,
        'count': count
    }
    print("添加" + name + "成功")


def shopping(commodityinf):
    '''
    购物
    :param commodityinf:
    :return:
    '''
    global budget
    global havebuy
    print("请输入您要购买的商品编号：")
    shoppingnum = input()
    print("请输入您要购买的商品数量")
    shoppingcount = int(input())
    if shoppingnum in commodityinf:
        if (int(commodityinf[shoppingnum]['count']) >= shoppingcount):
            if float((commodityinf[shoppingnum]['price']))*shoppingcount <= float(budget):
                budget -= float(commodityinf[shoppingnum]['price'])*shoppingcount
                commodityinf[shoppingnum]['count'] = int(commodityinf[shoppingnum]['count']) - shoppingcount
                havebuy[shoppingnum] = {
                    'name': commodityinf[shoppingnum]['name'],
                    'price': commodityinf[shoppingnum]['price'],
                    'count': shoppingcount
                }
                print("购买" + shoppingnum + "成功")
            else:
                print("您的预算已不足！")
        else:
            print("抱歉，商品库存已经不足！")
    else:
        print("你输入的商品编号有误！")


'''
主程序
'''
index = 0
usertable = {
    "root": "123456"
}
commodityinf = {
}
havebuy = {
}

print("欢迎来到三创购物系统！")
print("------------------")
while 1:
    print("请选择您要进行的操作：")
    print("1-注册")
    print("2-登录")
    select1 = input()
    if (select1 == '1'):
        register()
    elif (select1 == '2'):
        login()
    else:
        print("输入有误请重新输入!")

    '''
    登录之后
    '''
    if (index == 1):
        print("请输入您的预算：")
        budget = float(input())
        while 1:

            print("------------------")
            print("尊敬的" + nowuser + "用户，您已经登录成功，请选择您要进行的操作：")
            print("1-购物")
            print("2-查看商品信息")
            print("3-查看已购买信息")
            print("4-添加商品")
            print("exit-退出登录")

            select2 = input()
            if (select2 == '1'):
                shopping(commodityinf)
            elif (select2 == '2'):
                print(commodityinf)
            elif (select2 == '3'):
                print(havebuy)
            elif (select2 == '4'):
                commodity_add()
            elif (select2 == 'exit'):
                print("您已经购买如下商品")
                print(havebuy)
                text = "您的余额为：{}"
                print(text.format(budget))
                break
            else:
                print("您输入的选择有误！")
